package com.qf.weekexam.controller;
import com.qf.weekexam.entity.User;
import com.qf.weekexam.ex.UserException;
import com.qf.weekexam.service.UserService;
import com.qf.weekexam.vo.HelperPageVo;
import com.qf.weekexam.vo.ResultVo;
import com.qf.weekexam.vo.TableVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
@Slf4j
public class PlateformController {

    @Autowired
    UserService userService;

    @GetMapping("/getallByHelperPage")
    public TableVo<User> getAllUserByHelperPage(HelperPageVo helperPageVo){

        log.debug("传入的参数 helperPageVo={}", helperPageVo);

        //声明条数和页大小
        TableVo<User> vo = userService.getUserByHelper(helperPageVo);
        return vo;

    }


    @PostMapping("/add")
    public ResultVo addUser(User user) throws UserException {
        log.debug("入参为，user={}", user);
        userService.addUser(user);
        return ResultVo.ok();
    }

    @PostMapping("/del")
    public ResultVo delUser(Integer[] ids){
        log.debug("入参为，ids={}", ids);
        userService.delUsers(ids);
        return ResultVo.ok("删除成功");

    }

    @PostMapping("/update")
    public ResultVo updateUser(User user) throws UserException {

        log.debug("入参为 user={}", user);

        userService.updateUserById(user);

        return ResultVo.ok();
    }


    /**
     * 全量导出
     * @return
     */
    @GetMapping("/export")
    public ResultVo exportAllUser(){

        String filePath = userService.export();

        return ResultVo.exportAllUsers(filePath);
    }

    @GetMapping("/download")
    public void downFile(String filename, HttpServletResponse response) throws IOException {

        userService.downFile(filename, response);

    }

    @PostMapping("/input")
    public ResultVo inputfile(MultipartFile fileuploadandinsert) throws IOException {
        ResultVo vo =  userService.uploadFile(fileuploadandinsert);
        return vo;
    }

    @GetMapping("/photo")
    public ResultVo portrait(HttpSession session) {

        ResultVo vo = userService.getUserPhoto(session);
        return vo;
    }

    @GetMapping("/IoReadImage")
    public void downFile(HttpSession session, HttpServletResponse response) throws IOException {

        userService.getUserImage(session, response);

    }
}
